﻿#include "Player.h"


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////



_Player::_Player()
{
    _Player::setProp(-1);
    m_listWuJiangQu << new _WuJiang;
    //WuJiangQu->hide();
    m_eJiaoSe = JiaoSe_Null;
    m_nDuiWu = -1;
    init();
}

_Player::~_Player()
{
    clearAllWuJiang();
    //if(WuJiangQu != nullptr)
    //    delete WuJiangQu;
}

void _Player::init()
{
    _BaseObject::init();

    m_nHuiHeKaiShi = m_nPanDingJieDuan = m_nMoPaiJieDuan = m_nChuPaiJieDuan = m_nQiPaiJieDuan = m_nHuiHeJieShuJieDuan = 0;
//    ShouPaiCount = PanDingCount =ZhuangBeiCount =


    QList<_WuJiang*>::const_iterator ii;
    for (ii = m_listWuJiangQu.constBegin(); ii != m_listWuJiangQu.constEnd(); ++ii)
    {
        _WuJiang* pWuJiang = (_WuJiang*)(*ii);
        pWuJiang->init();
    }
    //m_listWuJiangQu.at(0)->init();

    //WuJiangCount = 0;
    m_nTiLi = m_nTiLiShangXian = 0;
    m_nSha = 1;
    setDead(false);
    m_bUsing = false; m_bTuoGuan = false; m_nChuPaiPoint = -1;
    //m_eJiaoSe = JiaoSe_Null;
    //m_nDuiWu = -1;

    m_listShouPai.clear();
    m_listZhuangBei.clear();
    m_listPanDing.clear();

    m_listJiNengPai.clear();
/*
    int i;
    for(i = 0; i < GAME_MAX_NUM_SLOT_SHOUPAI; i++)
    {
        GetShouPaiSlot(i)->iType = _BaseObject::Area_ShouPai;
        GetShouPaiSlot(i)->pData = nullptr;
        GetShouPaiSlot(i)->iFlag = 0;
    }
    for(i = 0; i < GAME_MAX_NUM_SLOT_ZHUANGBEI; i++)
    {
        GetZhuangBeiSlot(i)->iType = _BaseObject::Area_ZhuangBei;
        GetZhuangBeiSlot(i)->pData = nullptr;
        GetZhuangBeiSlot(i)->iFlag = 0;
    }
    for(i = 0; i < GAME_MAX_NUM_SLOT_PANDING; i++)
    {
        GetPandingSlot(i)->iType = _BaseObject::Area_PanDing;
        GetPandingSlot(i)->pData = nullptr;
        GetPandingSlot(i)->iFlag = 0;
    }
    for(i = 0; i < GAME_MAX_NUM_SLOT_WUJIANG; i++)
    {
        GetWujiangSlot(i)->iType = _BaseObject::Area_WuJiang;
    }
    */
    /*
    int i;
    for(i = 0; i < GAME_MAX_NUM_SLOT_SHOUPAI + GAME_MAX_NUM_SLOT_ZHUANGBEI + GAME_MAX_NUM_SLOT_PANDING; i++)
    {
        PaiQu[i] = nullptr;
    }*/

    //WuJiangQu->tilishangxian = -1;
}

int _Player::getWuJiang(_WuJiang::_Value wuJiangValue, int n)
{
    if(n < 0 || n >= m_listWuJiangQu.count())
        return -1;

    _WuJiang *wj = m_listWuJiangQu.at(n);

    wj->setProp(wuJiangValue);
    refreshJiNeng();

    //qDebug() << "选择武将:" << wuJiangValue;

    //*WuJiangQu = *wj;
    //重新绘制WuJiangQu!!!
    m_nTiLi = m_nTiLiShangXian = wj->maxBloodCount();
    emit s_SetBlood(m_nTiLi,m_nTiLiShangXian);
    emit s_qml_RefreshWuJiang();
    wj->m_bUsing = true;
    //WuJiangQu->setPosition(_BaseObject::Area_WuJiang,WuJiangCount,m_nID);
    //WuJiangCount++;
    return 0;
}


int _Player::getPaiCount(_Pai::_PaiValue pai,_BaseObject::_Area area)
{
    int n = 0;
    switch(area)
    {
    case _BaseObject::Area_ShouPai:
        for(int i = 0; i < m_listShouPai.length(); i++)
        {
            if(pai == _Pai::Value_All || getShouPaiData(i)->value() == pai)
                n++;
        }
        return n;
    case _BaseObject::Area_PanDing:
        for(int i = 0; i < m_listPanDing.length(); i++)
        {
            if(pai == _Pai::Value_All || getPandingData(i)->value() == pai)
                n++;
        }
        return n;
    case _BaseObject::Area_ZhuangBei:
        for(int i = 0; i < m_listZhuangBei.count(); i++)
        {
            if(pai == _Pai::Value_All || getZhuangBeiData(i)->value() == pai)
                n++;
        }
        return n;
    default:
        s_Critical(_T("Err getPaiCount1:%1").arg(area));
    }

    return -1;
}

int _Player::getPaiCount(_BaseObject::_Areas subAreas, _BaseObject::_Area area)
{
    int n = 0;
    switch(area)
    {
    case _BaseObject::Area_ZhuangBei:
        for(int i = 0; i < m_listZhuangBei.count(); i++)
        {
            if(((_Pai*)m_listZhuangBei.at(i))->positionArea() & subAreas)
                n++;
        }
        return n;
    default:
        s_Critical(_T("Err getPaiCount2:%1").arg(area));
    }

    return -1;
}

_Pai* _Player::getShouPaiData(int n)
{
    if(n >= 0 && n < m_listShouPai.length())//GAME_MAX_NUM_SLOT_SHOUPAI)
        return (_Pai*)m_listShouPai.at(n);
    else
    {
        s_Critical(_T("GetPaiData Err:%1").arg(n));
        return nullptr;
    }
}

_Pai* _Player::getJiNengPaiData(int n)
{
    if(n >= 0 && n < m_listJiNengPai.length())//GAME_MAX_NUM_SLOT_SHOUPAI)
        return (_Pai*)m_listJiNengPai.at(n);
    else
    {
        s_Critical(_T("GeJiNengData Err:%1").arg(n));
        return nullptr;
    }
}

_Pai* _Player::getZhuangBeiData(_BaseObject::_Area area)
{
    for(int i = 0; i < m_listZhuangBei.count(); i++)
    {
        if(((_Pai*)m_listZhuangBei.at(i))->positionValue() & area)
            return (_Pai*)m_listZhuangBei.at(i);
    }
    return nullptr;
    /*
    QObjectList::const_iterator ii;
    for (ii = m_listZhuangBei.constBegin(); ii != m_listZhuangBei.constEnd(); ++ii)
    {
        if(((_Pai*)ii)->type() == type)
            return ((_Pai*)ii);
    }
    return nullptr;
    */

}
_Pai* _Player::getZhuangBeiData(int n)
{
    if(n >= 0 && n < m_listZhuangBei.length())//GAME_MAX_NUM_SLOT_PANDING)
    {
        return (_Pai*)m_listZhuangBei.at(n);
    }
    else
    {
        s_Critical(_T("getZhuangBeiData Err:%1").arg(n));
        return nullptr;
    }
}
_Pai* _Player::getPandingData(int n)
{
    if(n >= 0 && n < m_listPanDing.length())//GAME_MAX_NUM_SLOT_PANDING)
    {
        return (_Pai*)m_listPanDing.at(n);
    }
    else
    {
        s_Critical(_T("getPandingData Err:%1").arg(n));
        return nullptr;
    }
}
_WuJiang* _Player::getWujiangData(int n)
{
    if(n >= 0 && n < GAME_MAX_NUM_SLOT_WUJIANG)//GAME_MAX_NUM_SLOT_PANDING)
    {
        return m_listWuJiangQu.at(n);
    }
    else
    {
        s_Critical(_T("GetWuJiangData Err:%1").arg(n));
        return nullptr;
    }
}

/*
GameSlot* _PLAYER::GetShouPaiSlot(int n)
{
    if(n >= 0 && n < ShouPaiCount)//GAME_MAX_NUM_SLOT_SHOUPAI)
        return ShouPai + n;
    else
    {
        s_Critical("GetShouPaiSlot Err");
        return nullptr;
    }
}
GameSlot* _PLAYER::GetZhuangBeiSlot(int n)
{
    if(n >= 0 && n < GAME_MAX_NUM_SLOT_ZHUANGBEI)
        return ShouPai + GAME_POKE_BOUND_ZHUANGBEI + n;
    else
    {
        s_Critical("GetZhuangBeiSlot Err");
        return nullptr;
    }
}
GameSlot* _PLAYER::GetPandingSlot(int n)
{
    if(n >= 0 && n < PanDingCount)//GAME_MAX_NUM_SLOT_PANDING)
    {
        return ShouPai + GAME_POKE_BOUND_PANDINGQU + n;
    }
    else
    {
        s_Critical("GetPandingSlot Err");
        return nullptr;
    }
}
GameSlot* _PLAYER::GetWujiangSlot(int n)
{
    return WuJiang + n;
}
*/

int _Player::getShouPai(_Pai *p)
{
    if(m_listShouPai.length() > GAME_MAX_NUM_SLOT_SHOUPAI)
    {
        s_Critical(_T("GetShouPaiLength Err:%1").arg(m_listShouPai.length()));
        return -2;
    }
    else if(m_listShouPai.length() == GAME_MAX_NUM_SLOT_SHOUPAI)
    {
        return -1;
    }

    if(p != nullptr)
    {
        p->setPosition(_BaseObject::Area_ShouPai,m_listShouPai.length(),m_nID);
        m_listShouPai.append(p);
        emit s_ShouPaiShu(m_listShouPai.length());
        //if(isMe())
            //p->setSomeFlags(_BaseObject::flag_ZhengMian);
        //ShouPaiCount++;
        return m_listShouPai.length();
    }
    return -3;
}

_Pai* _Player::getZhuangBei(_Pai *pai)
{
    _Pai* p = nullptr;
    for(int i = 0; i < m_listZhuangBei.count(); i++)  //找到同类,卸载并返回
    {
        //if(((_Pai*)m_listZhuangBei.at(i))->type() == type || type == _Pai::Type_All)
        if(((_Pai*)m_listZhuangBei.at(i))->type() == pai->type())
        {
            p = (_Pai*)(m_listZhuangBei.takeAt(i));
            p->setPosition(Area_Null,0,0);
            break;
        }
    }

    pai->setPosition(Area_ZhuangBei,Area_WuQi,m_nID); //装备
    m_listZhuangBei.append(pai);
    return p;
}

int _Player::getPanDingPai(_Pai *p)
{
    //QString str;
    if(m_listPanDing.length() > GAME_MAX_NUM_SLOT_PANDING)
    {
        s_Critical(_T("GetPanDingPaiLength Err:%1").arg(m_listPanDing.length()));
        return -1;
    }

    if(p != nullptr)
    {
        if(getPaiCount(p->value(),_BaseObject::Area_PanDing) > 0)    //已经有此判定牌
        {
            //s_Critical("GetPanDingPai Err2");
            return 0;
        }
        /*
        str = QString("%1装上判定牌%2")
            .arg(this->getWujiangData()->getName())
            .arg(p->getName());*/
        p->setPosition(_BaseObject::Area_PanDing,m_listPanDing.length(),m_nID);
        m_listPanDing.append(p);
        //PanDingCount++;
        return 1;
    }
    return -2;
}

bool _Player::containZhuangBei(_Pai::_PaiValue v)
{
    for(int i = 0; i < m_listZhuangBei.count(); i++)
    {
        if(v == _Pai::Value_All || ((_Pai*)m_listZhuangBei.at(i))->value() == v)
        {
            return true;
        }
    }
    return false;
}

bool _Player::containPanDing(_Pai::_PaiValue v)
{
    for(int i = 0; i < m_listPanDing.count(); i++)
    {
        if(v == _Pai::Value_All || ((_Pai*)m_listPanDing.at(i))->value() == v)
        {
            return true;
        }
    }
    return false;
}

bool _Player::containJiNeng(_JiNeng::_JiNengValue value)
{
    QObjectList::const_iterator ii;
    for (ii = m_listJiNengPai.constBegin(); ii != m_listJiNengPai.constEnd(); ++ii)
    {
        _Pai* p = (_Pai*)(*ii);
        if( p->containJiNeng(value) == true )
            return true;
    }

    return false;
}

_Pai* _Player::qiShouPai(int n, bool bTiaoZheng)
{
    if(n >= 0 && n < m_listShouPai.length())
    {
        _Pai *p = (_Pai*)m_listShouPai.at(n);
        m_listShouPai[n] = nullptr;
        if(p != nullptr)
        {
            //p->SetPaiPosition(_BaseObject::Area_Null,-1,-1);
            if(bTiaoZheng)
            {
                p->setPosition(_BaseObject::Area_Null,0,0);
                m_listShouPai.removeAt(n);
                tiaoZhengShouPaiDui();
            }
            return p;
        }
        else
        {
            s_Critical(_T("%1 QiPai Err(null)").arg(id()));
            return nullptr;
        }
    }
    else
    {
        s_Critical(_T("QiPai Err:%1").arg(n));
        return nullptr;
    }
}

_Pai* _Player::qiZhuangBei(_BaseObject::_Areas areas)
{
    _Pai* p = nullptr;

    for(int i = 0; i < m_listZhuangBei.count(); i++)
    {
        //if(((_Pai*)m_listZhuangBei.at(i))->type() == type || type == _Pai::Type_All)
        if(((_Pai*)m_listZhuangBei.at(i))->positionArea() & areas)
        {
            p = (_Pai*)(m_listZhuangBei.takeAt(i));
            p->setPosition(_BaseObject::Area_Null,0,0);
            break;
        }
    }
    return p;
}

_Pai* _Player::qiPanDing(int n)
{
    if(n >= 0 && n < m_listPanDing.length())
    {
        _Pai *p = (_Pai*)m_listPanDing.takeAt(n);
        if(p != nullptr)
        {
            //p->SetPaiPosition(_BaseObject::Area_Null,-1,-1);
            //tiaoZhengPanDingQu();
            p->setPosition(_BaseObject::Area_Null,0,0);
            return p;
        }
        else
        {
            s_Critical(_T("QiPanDing Err(null)"));
        }
        //pGame->QiPai(p);
        //ShouPaiCount = TiaoZhengPaiDui();
    }
    else
    {
        s_Critical(_T("QiPanDing Err:%1").arg(n));
    }
    return nullptr;
}

/*new
int _Player::tiaoZhengPanDingQu()
{
    QVariant v;
    v.setValue<_Pai*>(nullptr);
    m_listPanDing.removeAll(v);  //List列表操作
}
*/

int _Player::tiaoZhengShouPaiDui()
{
    int i = 0;
    //m_listShouPai.removeAll(v);  //List列表操作
    while(i < m_listShouPai.count())
    {
        _Pai* p = (_Pai*)(m_listShouPai.at(i));
        if(p == nullptr)
        {
            m_listShouPai.removeAt(i);
            if(i < m_nChuPaiPoint)    //调整chupai指针
                m_nChuPaiPoint--;
            continue;
        }
        p->setPosition(p->positionArea(),i,p->positionPlayer());
        i++;
    }
    emit s_ShouPaiShu(m_listShouPai.length());
    return m_listShouPai.length();
}

/*
int _Player::tiaoZhengPanDingQu()
{
    int i = 0, j = 0;    //i指向下一个空位
    for(; j < PanDingCount; )
    {
        if(m_listPanDing[j] == nullptr)    //j没有牌
        {
            j++;
        }
        else            //j有牌
        {
            if(i != j)  //移动
            {
                m_listPanDing[i] = m_listPanDing[j];
                m_listPanDing[j] = nullptr;
            }
            m_listPanDing[i]->setPosition(_BaseObject::Area_PanDing,i,m_nID);
            i++;
            j++;
        }
    }
    / *
    for(;i < GAME_MAX_NUM_SLOT_SHOUPAI;i++)        //剩下牌清NULL
    {
        ShouPai[i].pData = nullptr;
    }
    * /
    return PanDingCount = i;
}

int _Player::tiaoZhengShouPaiDui()
{
    int i=0,j=0;    //i指向下一个空位
    //Debug("tiaozhengshoupaidui");
    for(; j < ShouPaiCount; )
    {
        if(ShouPaiQu[j] == nullptr)    //j没有牌
        {
            j++;
        }
        else            //j有牌
        {
            if(i != j)  //移动
            {
                ShouPaiQu[i] = ShouPaiQu[j];
                ShouPaiQu[j] = nullptr;
            }
            ShouPaiQu[i]->setPosition(_BaseObject::Area_ShouPai,i,m_nID);
            i++;
            j++;
        }
    }
    / *
    for(;i < GAME_MAX_NUM_SLOT_SHOUPAI;i++)        //剩下牌清NULL
    {
        ShouPai[i].pData = nullptr;
    }
    * /
    return ShouPaiCount = i;
    / *
    for(int i=0; i < POKE_SHOUPAI_MAXNUM;i++ )
    {
        if(_PLAYER[w].shoupai[i].leixing==-1)
        {
            for(int j=i; j < POKE_SHOUPAI_MAXNUM-1; j++)
            {
                if(_PLAYER[w].shoupai[j] == -1)
                    continue;
                _PLAYER[w].shoupai[j]=_PLAYER[w].shoupai[j+1];
            }
            _PLAYER[w].shoupai[j].leixing = -1;
            continue;
        }
    }

    for(int i=0; i < POKE_SHOUPAI_MAXNUM;i++ )
    {
        if(_PLAYER[w].shoupai[i].leixing==-1)
        {
            for(int j=i; j < POKE_SHOUPAI_MAXNUM-1; j++)
            {
                if(_PLAYER[w].shoupai[j] == -1)
                    continue;
                _PLAYER[w].shoupai[j]=_PLAYER[w].shoupai[j+1];
            }
            _PLAYER[w].shoupai[j].leixing = -1;
            continue;
        }
    }
    * /
}
*/

int _Player::flagShouPai(_BaseObject::_Flags flags, _Pai::_PaiValue ePai, _BaseObject::_FlagType type)
{
    int n = 0;
    //if(isMe())
    //    m_eFlags |= _BaseObject::flag_ZhengMian;
    for(int i = 0; i < m_listShouPai.length(); i++)
    {
        _Pai *p = ((_Pai*)m_listShouPai.at(i));
        if(p->value() == ePai || ePai == _Pai::Value_All)
        {
            p->setFlags(flags,type);
            n++;
        }
    }
    return n;
}

int _Player::flagZhuangBeiByValue(_BaseObject::_Flags flags, _Pai::_PaiValue pai, _BaseObject::_FlagType type)
{
    int n = 0;
    //if(isMe())
    //    m_eFlags |= _BaseObject::flag_ZhengMian;

    for(int i = 0; i < m_listZhuangBei.count(); i++)
    {
        if(pai == _Pai::Value_All || ((_Pai*)m_listZhuangBei.at(i))->value() == pai)
        {
            ((_Pai*)m_listZhuangBei.at(i))->setFlags(flags,type);
            n++;
        }
    }
    return n;
}

int _Player::flagZhuangBeiByArea(_Flags flags, _BaseObject::_Areas areas, _BaseObject::_FlagType type)
{
    int n = 0;
    //if(isMe())
    //    m_eFlags |= _BaseObject::flag_ZhengMian;

    for(int i = 0; i < m_listZhuangBei.count(); i++)
    {
        if(((_Pai*)m_listZhuangBei.at(i))->positionArea() & areas)
        {
            ((_Pai*)m_listZhuangBei.at(i))->setFlags(flags,type);
            n++;
        }
    }
    return n;
}

int _Player::flagPanDing(_BaseObject::_Flags flags, _Pai::_PaiValue ePai, _BaseObject::_FlagType type)
{
    int n = 0;
    //if(isMe())
    //    m_eFlags |= _BaseObject::flag_ZhengMian;
    for(int i = 0; i < m_listPanDing.length(); i++)
    {
        _Pai *p = ((_Pai*)m_listPanDing.at(i));
        if(p->value() == ePai || ePai == _Pai::Value_All)
        {
            p->setFlags(flags,type);
            n++;
        }
    }
    return n;
}

int _Player::flagJiNengPai(_BaseObject::_Flags flags, _Pai::_PaiValue ePai, _BaseObject::_FlagType type)
{
    int n = 0;
    //if(isMe())
    //    m_eFlags |= _BaseObject::flag_ZhengMian;
    for(int i = 0; i < m_listJiNengPai.length(); i++)
    {
        _Pai *p = ((_Pai*)m_listJiNengPai.at(i));
        if(p->value() == ePai || ePai == _Pai::Value_All)
        {
            p->setFlags(flags,type);
            n++;
        }
    }
    return n;
}
/*
int _Player::refreshAllFlags()
{
    setFlags(_BaseObject::flag_ZhengMian);
    flagZhuangBeiByValue(_BaseObject::flag_ZhengMian,_Pai::Value_All);
    flagPanDing(_BaseObject::flag_ZhengMian,_Pai::Value_All);
    if(isMe())
        flagShouPai(_BaseObject::flag_ZhengMian,_Pai::Value_All);
    else
        flagShouPai(_BaseObject::Flag_Null,_Pai::Value_All);

    return 0;
}

    //根据下标设置装备
    int flagZhuangBeiByValueById(int flags,int ID)
    {
        if(zhuangbei[ID].iValue == GAME_POKE_EMPTY)
            return -1;

        zhuangbei[ID].m_eFlags = flags;
        return 0;
    }



    //返回判定区的数目
    int PanDingShu()
    {
        int n=0;
        for(int i=0; i<GAME_MAX_NUM_SLOT_PANDING; i++)
            if( pandingpai[i].iValue != GAME_POKE_EMPTY)
                n++;

        return n;
    }

    //手牌数
    int ShouPaiShu()
    {
        int n=0;
        for(int i=0; i<GAME_MAX_NUM_SLOT_SHOUPAI; i++)
            if( shoupai[i].iValue != GAME_POKE_EMPTY)
                n++;

        return n;
    }

    //装备数
    int ZhuangBeiShu()
    {
        int n=0;
        for(int i=0; i<GAME_MAX_NUM_SLOT_ZHUANGBEI; i++)
            if( zhuangbei[i].iValue != GAME_POKE_EMPTY)
                n++;

        return n;
    }
*/
